
#include "spi_bus.h"

static uint8_t spi_bus_rw_byte(void *handle, uint8_t send_byte)
{
    spi_bus_handle_t *t = (spi_bus_handle_t *)handle;
    
    while(SET == spi_i2s_flag_get((spi_type *)(t->spi_base), SPI_I2S_BF_FLAG));
    spi_i2s_data_transmit((spi_type *)(t->spi_base), send_byte);
    while(SET == spi_i2s_flag_get((spi_type *)(t->spi_base), SPI_I2S_BF_FLAG));
    return spi_i2s_data_receive((spi_type *)(t->spi_base));
}

static void spi_bus_enable(void *handle, uint8_t enable)
{
    spi_bus_handle_t *t = (spi_bus_handle_t *)handle;

    hal_pin_write(&t->cs, enable==0);
}

spi_general_bus_t spi_general_bus = 
{
    .enable = spi_bus_enable,
    .rw_byte = spi_bus_rw_byte,
};
